# modules/squid/manifests/init.pp - manage squid stuff
# Copyright (C) 2007 admin@immerda.ch
#

# modules_dir { "at3_nfs": }

import "nfs"
import "crvpuppet"

define at3_nfssrv($webaddr="", $roleid="") {
  if $AutoMode==true and $CloudCRV==true {
    $NFSServerSharePath=get_role_attr($webaddr,$roleid,"NFSServerSharePath")    
    $NFSClientAddr=get_dependent_attrlist($webaddr,$roleid,"NFSClientAddr")
  }
  
  if ( $AutoMode == false ) or ( $AutoMode==true and $CloudCRV==true ) {

    $share_root = $NFSServerSharePath
    $clientlist = $NFSClientAddr
    $share_options = "rw,sync,no_root_squash"
    
    notice("mkdir -p $share_root")
    
    exec { create_share_root:
      path => "/usr/bin:/usr/sbin/:/bin:/sbin:/usr/local/bin:/usr/local/sbin",
      command => "mkdir -p $share_root",
      creates => "$share_root"
    }
    
    notice("creating $share_root")
    
    file { "${share_root}":
      ensure => directory,
      owner => "root",
      group => "root",
      mode => 755,
      require => Exec["create_share_root"],
    }
    
    file { "/etc/exports":
      owner => "root",
      group => "root",
      mode => 644,
      content => template("at3_nfs/etc-exports.tpl"),
      notify => Service["nfs"],
    }

    package { "nfs-utils":
      ensure => installed,
    }
    
    service { [ "nfs", "nfslock", "portmap"]:
      hasstatus => true,
      hasrestart => true,
      ensure => running,
      enable => true,
      require => [Package["nfs-utils"],File["${share_root}","/etc/exports"]]
    }

  }
  
  if $AutoMode==true and $CloudCRV==true {
    set_role_attr($webaddr,$roleid,"NFSServerAddr","$ipaddress:$NFSServerSharePath")
    set_role_attr($webaddr,$roleid,"NFSServerClientMountPath","$NFSServerSharePath")

    #set_role_attr($webaddr,$roleid,"ProfileApplied","True")
  }
}

define at3_nfsclient($webaddr="", $roleid="") {
  if $AutoMode==true and $CloudCRV==true {
    $NFSServerAddr=get_provider_attr($webaddr,$roleid,"NFSServerAddr")
    $NFSServerClientMountPath=get_provider_attr($webaddr,$roleid,"NFSServerClientMountPath")
  }
  
  if ( $AutoMode == false ) or ( $AutoMode==true and $CloudCRV==true ) {
    include nfs_client
    notice("Setting up NFS Share: '$NFSServerAddr' to '$NFSServerClientMountPath'")
    mount_nfs_share { $NFSServerClientMountPath:
      source => $NFSServerAddr, 
    }
  }
  
  if $AutoMode==true and $CloudCRV==true {
    set_role_attr($webaddr,$roleid,"NFSClientAddr",$ipaddress)

    #copy NFSServerClientMountPath to this client so dependent roles will know where to access
    set_role_attr($webaddr,$roleid,"NFSServerClientMount","$NFSServerClientMountPath")
    #set_role_attr($webaddr,$roleid,"ProfileApplied","True")
  }
}

define at3_nfssrv_reverse($webaddr, $roleid) {
  set_role_attr($webaddr,$roleid,"NFSServerAddr","")
  set_role_attr($webaddr,$roleid,"NFSServerClientMountPath","")

  #set_role_attr($webaddr,$roleid,"ReverseProfileApplied","True")
}

define at3_nfsclient_reverse($webaddr, $roleid) {
  set_role_attr($webaddr,$roleid,"NFSClientAddr","")

  #set_role_attr($webaddr,$roleid,"ReverseProfileApplied","True")
}
